import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { post } from '@/api/sys/connSysOpenApi'
import { registerNew, resetPwd } from '@/api/sys/user'

const user = {
  state: {
    token: getToken(),
    menus: {},
    info: {},
    roles: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_MENUS: (state, menus) => {
      state.info = menus
    }
  },

  actions: {
    // 重置密码
    Repwd({ commit }, RegisterInfo) {
      return new Promise((resolve, reject) => {
        resetPwd(RegisterInfo).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 用户注册
    Register({ commit }, RegisterInfo) {
      return new Promise((resolve, reject) => {
        registerNew(RegisterInfo).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取注册短信
    SendRegisterMsg({ commit }, phone) {
      return new Promise((resolve, reject) => {
        post(phone + '/code/register').then(response => {
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取注册短信
    SendRepwdMsg({ commit }, phone) {
      return new Promise((resolve, reject) => {
        post(phone + '/code/resetPwd').then(response => {
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {
          const data = response.data
          setToken(data)
          commit('SET_TOKEN', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          commit('SET_MENUS', response.menus)
          const userinfo = response.user
          commit('SET_INFO', userinfo)
          const userRoles = userinfo.roles
          const roles = []
          userRoles.forEach((val, index) => {
            roles.push(val.userType)
          })
          commit('SET_ROLES', roles)
          userinfo.roles = roles
          resolve(userinfo)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', false)
          commit('SET_INFO', {})
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        removeToken()
        commit('SET_TOKEN', false)
        commit('SET_ROLES', [])
        resolve()
      })
    }
  }
}

export default user
